#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include "ffmpeg.h"
static double get_time()
{
	struct timeval cur;
	gettimeofday(&cur, NULL);
	return cur.tv_sec + cur.tv_usec / 1000000.;
}
static char pbm_getc(file)
FILE* file;
{
  char ch;

  ch = getc(file);

  if (ch=='#')
  {
    do
    {
      ch = getc(file);
    }
    while (ch!='\n' && ch!='\r');
  }

  return ch;
}

static int pbm_getint(file)
FILE* file;
{
  char ch;
  int i;

  do
  {
    ch = pbm_getc(file);
  }
  while (ch==' ' || ch=='\t' || ch=='\n' || ch=='\r');

  i = 0;
  do
  {
    i = i*10 + ch-'0';
    ch = pbm_getc(file);
  }
  while (ch>='0' && ch<='9');

  return i;
}

int main()
{
int argc=10;
char *argv[argc];
argv[0]="../ffmpeg-0.7-rc1/./ffmpeg";
argv[1]="-r";
argv[2]="30";
argv[3]="-i";
argv[4]="r%d.pgm";
argv[5]="-r";
argv[6]="30";
argv[7]="-b";
argv[8]="25k";
argv[9]="try.mp4";
int itis=65535;
printf("HERE IT IS...........................=%d\n",itis);
printf("kdslalkf\n");

  	data=malloc(FREENECT_DEPTH_11BIT_SIZE*30);
	output=malloc(300000);
 	FILE *fd;
	int i,j;
	char name[25];
	char tplorg[25]="./depthverify/d%d.pgm"; 
for( i =0;i<30;i++){
	sprintf(name,tplorg,i);
	if (!(fd = fopen(name,"rb"))){
    	printf("Couldn't open %s\n",name);
		exit(0);
   	}		
	getc(fd); getc(fd); // magic number (P6) 
  	printf("%d_%d_%d\n",pbm_getint(fd), pbm_getint(fd), pbm_getint(fd)); // width height maxcolors
	for(j=0;j<FREENECT_DEPTH_11BIT_SIZE;j++){
		int p=getc(fd);
		if(p<0){
			p=p+256;
			printf("here it is!\n");		
		}
		*(data+i*FREENECT_DEPTH_11BIT_SIZE+j)=(unsigned char)p;
//printf("%d_",(char)p);
	}
	fclose(fd);
}
//printf("%c_%d_%c_%d_%c_%d_%c_%d_ptr=%d\n",*(data),*(data),*(data+1),*(data+1),*(data+2),*(data+2),*(data+3),*(data+3),data);
	
	//printf("%c_%d_%c_%d_%c_%d_%c_%d_ptr=%d\n",*(data+3*FREENECT_VIDEO_RGB_SIZE),*(data+3*FREENECT_VIDEO_RGB_SIZE),*(data+1+3*FREENECT_VIDEO_RGB_SIZE),*(data+1+3*FREENECT_VIDEO_RGB_SIZE),*(data+2+3*FREENECT_VIDEO_RGB_SIZE),*(data+2+3*FREENECT_VIDEO_RGB_SIZE),*(data+3+3*FREENECT_VIDEO_RGB_SIZE),*(data+3+3*FREENECT_VIDEO_RGB_SIZE),data);
double start_time=get_time();
outSize=0;
num_of_frames_to_encode=30;
printf("going to compress\n\n");
isDepth=1;
myffmpeg(argc,argv);
printf("Time Taken=%f\n",start_time-get_time());
printf("--------outSize=%d-------------------------------------------------\n",outSize);

/*	int outSize=mpeg2enc(data);*/

	FILE *f;
  if (!(f=fopen("mynew.mp4","wb")))
  {
 printf("Couldn't create output file %s",argv[2]);
    exit(0);
  }
//output=output-outSize;
for(i=0;i<outSize;i++,output++){
	putc(*output,f);
}
  fclose(f);

	return 0;
}

